<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/9 0009
 * Time: 下午 2:39
 */
namespace app\api\controller;
use think\Loader;
use think\Request;

class Question extends App
{


    /**
     * 测评列表
     */
    public function index()
    {
		$cat = db('ceping_cat')->field('id,name')->where('is_show',1)->order('sort asc')->select();
		foreach ($cat as $kk => $vv)
		{
			$question_list = db('ceping')->field('id,name,question_remark,cover,cp_num,cp_time,algorithm')->where('is_show',1)->where('cat_id',$vv['id'])->order('sort asc')->select();
			
			$question_list = array_map(function ($rs){
				$rs['cover'] = str_replace("\\","/",config('app_host').$rs['cover']);
				$timu_list = service("ceping\\".$rs['algorithm'])->generateTest();
				$rs['question_nums'] = count($timu_list);
				unset($rs['algorithm']);
				return $rs;
			}, $question_list);	
			$cat[$kk]['question_list'] = $question_list;	
			
		}
        return $this->show(1,"success", $cat);
    }

    /**
     * 获取题目详情
     */
    public function get_detail()
    {
        $question_id = input('question_id');
        if(!$question_id) return $this->show(2001,'缺少参数question_id');
        $info = db('ceping')->field('id,name,question_remark,cover,cp_num,cp_time,question_content')->where('id',$question_id)->where('is_show',1)->order('sort asc')->find();
		$info['cover'] = str_replace("\\","/",config('app_host').$info['cover']);
        return $this->show(1,"success", $info);
    }
	
	//开始测评
    public function get_question_list()
    {
        $question_id = input('question_id',1);
        $where = ['is_show'=>1];
        if(!$question_id) return $this->show(2001,'缺少参数question_id');
		
		
        $info = db('ceping')->field('id,name,algorithm')->where('id',$question_id)->where('is_show',1)->order('sort asc')->find();
        if(empty($info)) return $this->show(2001,'测评不存在');
		
        
        $QuestionService = service("ceping\\".$info['algorithm']);
		$list = $QuestionService->generateTest();
		$list_new = [];
		foreach ($list as $kk => $vv)
		{
			if($question_id == 5){
				$options = [0=>'从未',1=>'几天',2=>'超过一周',3=>'几乎每天'];
			}else{
				$options = [1=>'完全不符合',2=>'不太符合',3=>'不确定',4=>'比较符合',5=>'完全符合'];
			}
			
			$list_new[$kk] = ['id'=>$vv['id'],'title'=>$vv['text'],'option'=>$options];
			
		}
		
        return $this->show(1,"success", $list_new);
    }
	
    public function finish_question()
    {
        $question_id = input('question_id');
        $order_id = input('order_id');
        if(!$question_id) return $this->show(2001,'缺少参数question_id');
		$answers = input('answers');
        if(!$answers) return $this->show(2001,'缺少参数answers');
		$answer_arr = json_decode($answers,true);
        if(empty($answer_arr)) return $this->show(2001,'请先作答');
      
        $info = db('ceping')->field('id,name,algorithm')->where('id',$question_id)->where('is_show',1)->order('sort asc')->find();
        if(empty($info)) return $this->show(2001,'测评不存在');
		//调用算法
        $QuestionService = service("ceping\\".$info['algorithm']);
		$result = $QuestionService->calculateScore($answer_arr);
		$result_json = json_encode($result,JSON_UNESCAPED_UNICODE);
		
		$flag = db('ceping_log')->where('question_id',$question_id)->where('uid',$this->user_id)->field('id,order_id')->find();
		if(!empty($flag)){
			if(empty($order_id) && !empty($flag['order_id'])){
				$order_id = $flag['order_id'];
			}
			$flag = Db('ceping_log')->where('id',$flag['id'])->update(['title'=>$info['name'],'answers'=>$answers,'result_json'=>$result_json,'order_id'=>$order_id,'update_time'=>time()]);
		}else{
			$flag = Db('ceping_log')->insertGetId(['uid'=>$this->user_id,'question_id'=>$question_id,'title'=>$info['name'],'answers'=>$answers,'result_json'=>$result_json,'order_id'=>$order_id,'create_time'=>time(),'update_time'=>time()]);
		}
		
		
		if($flag){
			$data_return = ['cp_id'=>$flag,'cp_result'=>$result];
			return $this->show(1,"success",$data_return);
		}else{
			return $this->show(2002,'接口出错');
		}
    }
	
    public function get_result()
    {
        $cp_id = input('cp_id');
        if(!$cp_id) return $this->show(2001,'缺少参数cp_id');
        $info = db('ceping_log')->field('id,result_json')->where('id',$cp_id)->find();
        if(empty($info)) return $this->show(2001,'测评不存在');
		$info['result_json'] = json_decode($info['result_json'],true);
        return $this->show(1,"success", $info);
    }
	
	
}